package com.lllteam.exam.account.library.provider

import android.content.ContentProvider
import android.content.ContentUris
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.net.Uri

class AccountProvider: ContentProvider() {
    companion object {
        const val COURSE_PATH = "account"
        const val AUTHORITIES = "com.lllteam.exam.account.course"

        val BASE_URI =  Uri.parse("content://$AUTHORITIES")
        val COURSE_URI = Uri.withAppendedPath(BASE_URI, COURSE_PATH)

        private const val PROVIDE_COURSE = 1

        const val _ID = "_id"
        const val NAME = "name"
        const val EN_NAME = "en_name"
        const val GRADE = "grade"
        const val MOBILE = "mobile"
        const val SEQ_ID = "seq_id"
        const val SCHOOL = "school"
        const val ADDRESS = "address"

        val matcher = UriMatcher(UriMatcher.NO_MATCH)
    }

    init {
        matcher.addURI(AUTHORITIES, COURSE_PATH, PROVIDE_COURSE)
    }

    lateinit var dbOpenHelper: DBOpenHelper
    private lateinit var db: SQLiteDatabase

    override fun onCreate(): Boolean {
        dbOpenHelper = DBOpenHelper(context!!)
        db = dbOpenHelper.writableDatabase
        return true
    }

    override fun query(
        uri: Uri,
        projection: Array<out String>?,
        selection: String?,
        selectionArgs: Array<out String>?,
        sortOrder: String?
    ): Cursor? {
       return when(matcher.match(uri)) {
           PROVIDE_COURSE -> {
               return db.query(
                   DBOpenHelper.TABLE_NAME,
                   projection,
                   selection,
                   selectionArgs,
                   null,
                   null,
                   sortOrder
               )
           }
            else  -> {
                null
            }
        }
    }

    override fun getType(p0: Uri): String? {
        return null
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        var _uri: Uri? = null
        when(matcher.match(uri)) {
            PROVIDE_COURSE -> {
                val rowId = db.insert(DBOpenHelper.TABLE_NAME, "", values!!)
                if (rowId > 0) {
                    _uri = ContentUris.withAppendedId(COURSE_URI, rowId)
                    context?.contentResolver?.notifyChange(_uri, null)
                }
            }
        }
        return _uri
    }

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int {
        var count = 0
        when(matcher.match(uri)) {
            PROVIDE_COURSE -> {
                count = db.delete(DBOpenHelper.TABLE_NAME, selection, selectionArgs)
            }
        }
        context?.contentResolver?.notifyChange(uri, null)
        return count
    }

    override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?): Int {
        var count = 0
        when(matcher.match(uri)) {
            PROVIDE_COURSE -> {
              count = db.update(DBOpenHelper.TABLE_NAME, values, selection, selectionArgs);
            }
        }
        context?.contentResolver?.notifyChange(uri, null)
        return count
    }

}